#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@File Name  : switchPlatfrom.py
@Author     : LeeCQ
@Date-Time  : 2020/8/29 11:18

切换平台
"""
import time
import unittest

from common import *
from exc import NotLoginError
from login import login_easy


class TestPlat(unittest.TestCase):
    """"""

    def setUp(self) -> None:
        self.driver = login_easy('test', '123456')
        self.plat = '自动测试机构'

    def tearDown(self) -> None:
        self.driver.close()

    def test_plat_from(self):
        d = switch_plat_from(self.driver, self.plat)
        self.assertEqual(d.find_element_by_css_selector('#main-hd-title > a:nth-child(2)').text.strip(),
                         self.plat,
                         '切换平台成功'
                         )


def switch_plat_from(_driver: BaseWebDriver, plat_name):
    """已经登录的WebDriver对象

    :param _driver:
    :param plat_name: 需要跳转到的机构名
    :return: 跳转到 {plat_name} webDriver
    """
    _driver.implicitly_wait(8)

    if is_login(_driver) is False:  # From common.py
        raise NotLoginError('检查登录状态')

    _driver.find_element_by_css_selector('#topnav > li:nth-child(3)').click()
    _driver.switch_to.frame(_driver.find_element_by_id('get'))
    _driver.find_element_by_css_selector('#name > div').click()
    _driver.find_element_by_class_name('bbit-tree-root').find_element_by_xpath(f'//*[@title="{plat_name}"]').click()
    _driver.switch_to.default_content()
    _driver.find_element_by_class_name('layui-layer-btn0').click()
    time.sleep(1)  # 需要强制等待页面跳转
    return _driver


if __name__ == '__main__':
    unittest.main()
